
   use "data/raw/styl_outcomes.dta", clear

      format  id %12.0g
      tostring id, gen(partid)



**************    Case by Case CORRECTIONS       *************************************

*Found alive and completed 2 surveys.
*Fixing this case surveyed twice but marked as deceased.

   replace r1_outcome = 1 if id == 74488725
   replace r2_outcome = 1 if id == 74488725


*Person originally found dead was found alive but not located for interview.

replace r1_outcome = 0                                if id == 74478203
replace r2_outcome = 0                                if id == 74478203
replace outcome = 0                                   if id == 74478203

replace r1_deceased = 0                               if id == 74478203
replace r2_deceased = 0                               if id == 74478203


*One person is marked as status = Imprisoned but also marked as deceased in in r1_deceased and r2_deceased.
*During death validation exercises they were confirmed in prison.

   replace r1_deceased = 0    if  id == 30358
   replace r2_deceased = 0    if  id == 30358
   replace r2_imprisoned = 1  if  id == 30358

* Some surveys made by phone (Phone / Expat surveys) were recorded as an outcome of its own.
* For simplicity we will count them as answering the survey once.
* It must be moted that these surveys were coded manually.

   replace r1_outcome = 1 if r1_outcome == 7
   replace outcome    = 1 if outcome    == 7

  *These two lines should do the same than the previous two.
   replace outcome = 1  if survey_status_phone == 1
   replace r1_outcome = 1  if survey_status_phone == 1

   *4 cases that are wrongly labeled as as not completing the 2nd round in the variable r2_outcome,
   *though because the variable outcome is coded properly this change does not change our estimates.

   replace r2_outcome = 1 if id == 20014
   replace r2_outcome = 1 if id == 20313
   replace r2_outcome = 1 if id == 30064
   replace r2_outcome = 1 if id == 30324

   *2 of those cases are marked as imprisoned buy they were released and then surveyed (hence no attrition!)
   replace r2_imprisoned = 0 if id == 20014
   replace r2_imprisoned = 0 if id == 20313

* The team also made what they call "Combined surveys".
* These are special surveys for people who were originally hard to track and coded manually.
* In the following chunk I correct those cases, similar to the phone/expat survey.

    replace r1_outcome = 1 if id == 30015
    replace r1_outcome = 1 if id == 30195
    replace r1_outcome = 1 if id == 30446
    replace r1_outcome = 1 if id == 30465

    replace outcome = 1 if id == 30015
    replace outcome = 1 if id == 30195
    replace outcome = 1 if id == 30446
    replace outcome = 1 if id == 30465

*Correcting some info for not consented surveys.
   replace r1_outcome = 5 if id == 30375
   replace r1_outcome = 5 if id == 20381
   replace r1_outcome = 5 if id == 20012


*Correcting general statements:
*If you had a first survey,  you should not be considered as attrition due to prison or death in your first survey.
*If you had a second survey, you should not be considerd as attrition due to prision or death in your second survey.


*This is probably due to the team updating the main status (r1_outcome, r2_outcome, outcomes) but not
*necessairily updating the imprision deceseaded variables.

   replace r1_imprisoned = 0 if r1_outcome == 1
   replace r1_deceased = 0  if r1_outcome == 1

   replace r2_imprisoned = 0 if r2_outcome == 1
   replace r2_deceased = 0 if r2_outcome == 1

   replace r1_imprisoned = 1 if r1_outcome == 4
   replace r2_imprisoned = 1 if r2_outcome == 4



*Similarly, we cannot consider attrition due to leaving the country if we actually surveyed the person.
*Phone surveys were coded by hand and that's why these are not properly updated.
   replace r1_leftliberia = 0 if r1_outcome == 1


*One person is marked as leaving the country for r2, and has no survey for r1,
*I will code that they had already left the country for r1 and code that type of attrition.

   replace r1_outcome = 6     if outcome == 6  & id == 20048
   replace r1_leftliberia = 1 if outcome == 6  & id == 20048


**************    END CORRECTIONS       *************************************






************ RECLASSIFIFYING DEATHS  ***************************************

*Generating new variable to reclassify deaths
gen death_type = ""


*Ebola or Other illness or disease ---> Illness
replace death_type = "Illness" if causeofdeath == 7 | causeofdeath == 4

*Accident -> Accidental Death
replace death_type = "Accidental Death" if causeofdeath == 1

*African Sign -> Suspected Witchcraft or Poison
replace death_type = "Suspected Witchcraft and Poison" if causeofdeath == 5


* Homicide or Mob violence -> Violent Death
replace death_type = "Violent Death" if causeofdeath == 2 | causeofdeath == 6

* Dont Know Refuse to Answer -> Unknown
replace death_type = "Unknown" if causeofdeath == -888 | causeofdeath == -999



*One case coded as other illness despite being beaten to death. This is now Violent Death.
replace death_type = "Violent Death" if id == 30268

*There is one case that is coded as labor complication --we move this to accidental death.
replace death_type = "Accidental Death" if id == 74313511



*replacing those labeled as others:
replace death_type = "Illness"                         if causeofdeath_other == "Tuberculosis"
replace death_type = "Illness"                         if causeofdeath_other == "T. B"
replace death_type = "Illness"                         if causeofdeath_other == "Chronic Sore on his leg"
replace death_type = "Illness"                         if causeofdeath_other == "Complicated illness"
replace death_type = "Illness"                         if causeofdeath_other == "Liver problem"
replace death_type = "Illness"                         if causeofdeath_other == "Severe Cold"
replace death_type = "Illness"                         if causeofdeath_other == "Chronic cold/ Tuberculosis"
replace death_type = "Illness"                         if causeofdeath_other == "Tubarculosis(TB)"
replace death_type = "Illness"                         if causeofdeath_other == "He was sick and died"
replace death_type = "Illness"                         if causeofdeath_other == "TB"
replace death_type = "Illness"                         if id == 74555987 /// Long comment about sickness ///



replace death_type = "Suspected Witchcraft and Poison" if causeofdeath_other == "Poison"

replace death_type = "Drugs"                           if causeofdeath_other == "Substance abuse"
replace death_type = "Drugs"                           if causeofdeath_other == "Drugs abuse and neglect and other illnesses"
replace death_type = "Drugs"                           if id == 74487375  //// Dead from Koko (drugs) ///


replace death_type = "Violent Death"                   if id == 20156     /// Got knifed in a fight with a coconut seller   ///

replace death_type = "Violent Death"                   if id == 20362     /// Got knifed by some gangsters    ///

replace death_type = "Violent Death"                   if id == 30047     /// Complication after severe fighting injuries  ///

replace death_type = "Violent Death"                   if id == 30115     /// Internal bleeding after getting beaten ///

replace death_type = "Unknown"                         if id == 30127     /// Father didn't know cause of death ///


replace death_type = "Accidental Death"               if causeofdeath_other == "Got drowned"
replace death_type = "Accidental Death"               if causeofdeath_other == "Electrical shocked"


*3 cases founded alive after. check first lines above
replace death_type = ""                               if id == 74488725
replace death_type = ""                               if id == 30358
replace death_type = ""                               if id == 74478203


***************************************************
** Creating binary versions of some death/attrition events
****************************************************

gen violent_death     = (death_type == "Violent Death")
gen non_violent_death = (death_type != "Violent Death" & death_type != "")

gen not_found =      (outcome == 0)
gen accident_death = (death_type == "Accidental Death")
gen poison_death   = (death_type == "Suspected Witchcraft and Poison")
gen illness_death  = (death_type == "Illness")
gen drug_death     = (death_type == "Substance Abuse")
gen unknown_death  = (death_type == "Unknown")

gen mob_violence_death = (causeofdeath == 6)
gen other_violent_death = (violent_death == 1 & causeofdeath != 6)


save "data/clean/STYL_Deaths.dta", replace
preserve

****************************************
********** Combining old and updated data
****************************************

qui do "${direc}/dofiles/preindex/comparing_death_sources.do"


********************************
* Using clean data
********************************

restore
*merging detailed death survey data
merge 1:1 partid using "data/clean/STYL_updated_deaths.dta", nogen keep(matched master)


gen new_violent_death     = (updated_death_type == "Violent Death")
gen new_non_violent_death = (updated_death_type != "Violent Death" & updated_death_type != "")


*There was a comment here making a note about partid == 20187 who is recorded as dead despite having 2 surveys.
*Since we verified both claims it seems he was reported dead after being surveyed.



**************************************************************************



replace r1_deceased = 0    if outcome == 1  | outcome == 2
replace r1_imprisoned = 0  if outcome == 1  | outcome == 2
replace r1_leftliberia = 0 if outcome == 1  | outcome == 2

replace r2_deceased = 0    if outcome == 2
replace r2_imprisoned = 0  if outcome == 2
replace r2_leftliberia = 0 if outcome == 2
replace r2_leftliberia = 1 if r2_outcome == 6 | r2_outcome == 7



      rename  r1_deceased      attrit_decease1
      rename  r1_imprisoned    attrit_imprisonment1
      rename  r1_leftliberia   attrit_outofcountrytown1

      gen  attrit_refusal1   = r1_outcome   == 5
      gen  attrit_noinfo1    = r1_outcome   == 0

      rename  r2_deceased      attrit_decease2
      rename  r2_imprisoned    attrit_imprisonment2
      rename  r2_leftliberia   attrit_outofcountrytown2

      gen  attrit_refusal2   = r2_outcome   == 5
      gen  attrit_noinfo2    = r2_outcome   == 0


       gen one_survey       = outcome==1
       gen both_survey      = outcome==2

      label define yesno 1 "Yes" 0 "No"


   keep partid  attrit_decease1 attrit_imprisonment1 attrit_outofcountrytown1 attrit_refusal1 attrit_noinfo1 ///
                attrit_decease2 attrit_imprisonment2 attrit_outofcountrytown2 attrit_refusal2 attrit_noinfo2 one_survey both_survey ///
                death_type violent_death non_violent_death updated_death_type drug_indicator type_violent_death new_violent_death new_non_violent_death

      reshape  long  attrit_decease attrit_imprisonment attrit_outofcountrytown attrit_refusal attrit_noinfo, i(partid one_survey both_survey death_type updated_death_type drug_indicator type_violent_death ) j(round)

    replace round = 7 if round ==1
    replace round = 8 if round ==2



   tempfile attrit_9yr
   save `attrit_9yr'


   use "data/prev/STYL_adm.dta", clear

   tostring partid, replace force

   keep partid phase p1 p2 p3
      expand 7 if p1 ==1
      expand 6 if p1 ==0

   * create round dummy

   * until now, the round variable stands for the number of times the person was surveyed.

   * changing it to account for time after treatment, For example, phase 1 was surveyed 7 times, and phase 2 & 3 were surveyed 6 times. However, phase 1 was not surveyed 4 weeks after treatment (new round =2) and phase 2 and 3 was not surveyed 5 and 7 months after treatment (new round = 3 and 4)

      bys partid: gen round =_n
         replace round = 8 if round ==7 & p1 == 1
         replace round = 7 if round ==6 & p1 == 1
         replace round = 6 if round ==5 & p1 == 1
         replace round = 5 if round ==4 & p1 == 1
         replace round = 4 if round ==3 & p1 == 1
         replace round = 3 if round ==2 & p1 == 1

         replace round = 8 if round ==6 & p2 == 1
         replace round = 7 if round ==5 & p2 == 1
         replace round = 6 if round ==4 & p2 == 1
         replace round = 5 if round ==3 & p2 == 1

         replace round = 8 if round ==6 & p3 ==1
         replace round = 7 if round ==5 & p3 ==1
         replace round = 6 if round ==4 & p3 ==1
         replace round = 5 if round ==3 & p3 ==1

    merge 1:1 partid round using "data/prev/STYL_attrit.dta", nogen keep(matched master)

    foreach var of  varlist attrit_decease attrit_imprisonment attrit_refusal attrit_outofcountrytown attrit_noinfo attrit_mentality {
      replace `var'=0 if `var'==.
    }

    merge 1:1 partid round using `attrit_9yr', nogen  update replace

    gen attrit_no_9y     = both_survey==1 | one_survey==1
    gen attrit_all_9y    = both_survey==0 & one_survey==0

    gen attrit_all = (attrit_decease==1 | attrit_imprisonment==1 | attrit_refusal==1 | attrit_noinfo==1 | attrit_mentality==1)

   gen attrit_allnomort = attrit_all
    replace attrit_allnomort = 0  if attrit_decease==1


 local roundst1 = 1
 local roundst2 = 2
 local roundlt1 = 5
 local roundlt2 = 6
 local round9y1 = 7
 local round9y2 = 8


 foreach x in "st" "lt" "9y"{

    bys partid: egen attrit_aux_decease_`x'           = sum(attrit_decease)          if round ==`round`x'1' | round ==`round`x'2'
    bys partid: egen attrit_aux_imprisonment_`x'      = sum(attrit_imprisonment)     if round ==`round`x'1' | round ==`round`x'2'
    bys partid: egen attrit_aux_refusal_`x'           = sum(attrit_refusal)          if round ==`round`x'1' | round ==`round`x'2'
    bys partid: egen attrit_aux_outofcountrytown_`x'  = sum(attrit_outofcountrytown) if round ==`round`x'1' | round ==`round`x'2'
    bys partid: egen attrit_aux_noinfo_`x'            = sum(attrit_noinfo)           if round ==`round`x'1' | round ==`round`x'2'
    bys partid: egen attrit_aux_all_`x'               = sum(attrit_all)              if round ==`round`x'1' | round ==`round`x'2'

    gen attrit_any_decease_`x'            = attrit_aux_decease_`x'          > 0 if !missing(attrit_aux_decease_`x')
    gen attrit_any_imprisonment_`x'       = attrit_aux_imprisonment_`x'     > 0 if !missing(attrit_aux_imprisonment_`x')
    gen attrit_any_refusal_`x'            = attrit_aux_refusal_`x'          > 0 if !missing(attrit_aux_refusal_`x')
    gen attrit_any_outofcountrytown_`x'   = attrit_aux_outofcountrytown_`x' > 0 if !missing(attrit_aux_outofcountrytown_`x')
    gen attrit_any_noinfo_`x'             = attrit_aux_noinfo_`x'           > 0 if !missing(attrit_aux_noinfo_`x')
    gen attrit_any_all_`x'                = attrit_aux_all_`x'              > 0 if !missing(attrit_aux_all_`x')

    gen attrit_both_decease_`x'           = attrit_aux_decease_`x'          ==2 if !missing(attrit_aux_decease_`x')
    gen attrit_both_imprisonment_`x'      = attrit_aux_imprisonment_`x'     ==2 if !missing(attrit_aux_imprisonment_`x')
    gen attrit_both_refusal_`x'           = attrit_aux_refusal_`x'          ==2 if !missing(attrit_aux_refusal_`x')
    gen attrit_both_outofcountrytown_`x'  = attrit_aux_outofcountrytown_`x' ==2 if !missing(attrit_aux_outofcountrytown_`x')
    gen attrit_both_noinfo_`x'            = attrit_aux_noinfo_`x'           ==2 if !missing(attrit_aux_noinfo_`x')
    gen attrit_both_all_`x'               = attrit_aux_all_`x'              ==2 if !missing(attrit_aux_all_`x')

 }

drop phase p1 p2 p3


save "data/clean/STYL_all_attrition.dta", replace
cd $direc
